qx.Class.define("caraffa.forms.ChartCPC",
{
	extend : qx.ui.form.ow.FormTab,
	construct : function () {
		this.base(arguments, "ChartCPC", "Consultas, Practicas y Cirugias");
		
		this._cmbObraSocial = new qx.ui.form.ow.ComboBox("Obra Social:");
//			this._cmbObraSocial.getLabel().setWidth(80);
		this._cmbPlan = new qx.ui.form.ow.ComboBox("Plan:");
//			this._cmbPlan.getLabel().setWidth(50);
		this._btnMostrar = new qx.ui.form.Button("Mostrar");
		this._txdDesde = new qx.ui.form.ow.DateText("Desde:");
//			this._txdDesde.getLabel().setWidth(50);
//			this._txdDesde.getDateText().setWidth(100);
		this._txdHasta = new qx.ui.form.ow.DateText("Hasta:");
//			this._txdHasta.getLabel().setWidth(50);
//			this._txdHasta.getDateText().setWidth(100);
		
		this._rdbAgrupar = new qx.ui.form.ow.Radios("Agrupar Consultas, Practicas y Cirugias:", [{value:'N', label:'No'}, {value:'S', label:'Si'}], true);
		var grpForm = new qx.ui.groupbox.GroupBox();
		grpForm.setLayout(new qx.ui.layout.VBox(5));
		
		this._attachEvents();
		this._initForm();
		
		grpForm.add(this._cmbObraSocial);
		grpForm.add(this._cmbPlan);
		grpForm.add(this._txdDesde);
		grpForm.add(this._txdHasta);
		grpForm.add(this._rdbAgrupar);
		grpForm.add(this._btnMostrar);
		this.add(grpForm, {left:10, top:10});
		
//		alert(this._txdDesde.getValue().substr(0,2) + " - " + this._txdDesde.getValue().substr(3,2) + " - " + this._txdDesde.getValue().substr(6,4));
//		this.add(this._cmbObraSocial, {left:10, top:10});
//		this.add(this._cmbPlan, {left:320, top:10});
//		this.add(this._txdDesde, {left:600, top:10});
//		this.add(this._txdHasta, {left:770, top:10});
//		this.add(this._btnMostrar, {left:940, top:10});
	},
	members :
	{
		_attachEvents : function () {
			this._btnMostrar.addListener("execute", this._getDatosEstadisticos, this);
			this._cmbObraSocial.addListener("changeValue", this._getPlanes, this);
		},
		_initForm : function() {
			var rpc = new qx.io.remote.Rpc("services/", "estadisticos");
			this._cmbObraSocial.setNewValues(rpc.callSync("getObrasSociales"));
		},
		_getPlanes : function () {
			var p = {};
			p.os = this._cmbObraSocial.getValue();
			var rpc = new qx.io.remote.Rpc("services/", "estadisticos");
			this._cmbPlan.setNewValues(rpc.callSync("getPlanes", p));
		},
		_getDatosEstadisticos : function () {
			var p = {};
			p.os = this._cmbObraSocial.getValue();
			p.nombre_os = this._cmbObraSocial.getValueDisplay();
			p.plan = this._cmbPlan.getValue();
			p.nombre_plan = this._cmbPlan.getValueDisplay();
			p.desde = this._txdDesde.getValue();
			p.hasta = this._txdHasta.getValue();
			
			var rpc = new qx.io.remote.Rpc("services/", "estadisticos");
			rpc.setTimeout(10000);
			
			if (this._rdbAgrupar.getValue() == 'N') {
				var result = rpc.callSync("getConsultasPracticasCirugias", p);
			} else {
				var result = rpc.callSync("getAgrupados", p);
			}
			
			if (result.lineas.length) {
				
				var values = result.lineas;
				var demo = {
		            data: values,
					options: function($jqplot) {
						return {
		            		title: 	result.titulo,
		                	highlighter: {
			                    tooltipAxes: 'y',
			                    useAxesFormatters: true,
			                    showTooltipGridPosition: true,
			                    show: true
		                	},
		                	axes:{
			                    xaxis:{
			                        renderer:$jqplot.DateAxisRenderer,
			                        rendererOptions: {
			                        	tickRenderer:$jqplot.CanvasAxisTickRenderer
			                        },
			                        tickOptions:{
			                            formatString:'%m/%Y', 
			                            fontSize:'8pt',
			                            fontFamilly: 'sans-serif',
			                            angle:-20,
			                            enableFontSupport: true
			                        },
			                        min:result.fecha,
			                        tickInterval:'1 month'
			                    },
			                    yaxis:{
			                        tickOptions:{
			                            formatString:'$%.2f'
			                        }
			                    }
			                },
			                series:result.series,
			                cursor: {
			                    show: false
			                },
			                legend: { show:true, location: 'ne' }
			            }
					},
					plugins: [
						'highlighter',
						'dateAxisRenderer',
						'cursor'             
					]
	    		}
		    	var plot = new qxjqplot.Plot(
		            demo.data, demo.options, demo.plugins, demo.title
		        );
		    	
		        var win = new qx.ui.window.Window();
				win.addListener("resize", win.center, win);
				win.set({showMaximize:false, allowMaximize:false, showMinimize:false, showClose:true, modal:true, movable:false, resizable:false, showStatusbar:false});
				win.maximize();
				win.setLayout(new qx.ui.layout.Grow());
		        
		        var lyr = new qx.ui.groupbox.GroupBox();
		        lyr.setLayout(new qx.ui.layout.Grow());
		        win.add(lyr);
		        lyr.add(plot);
		        
		        win.addListener("close", function () {
		        	lyr = null;
		        	plot = null;
		        	win = null;
		        }, this);
				
				win.open();
			} else {
				alert("No se encontraron resultados para su consulta.");
			}
		}
	}
});
